package org.apache.osgimaker.analyse.algorithm.renderer;

import java.text.MessageFormat;

import org.apache.osgimaker.analyse.algorithm.ClassAttributes;
import org.apache.osgimaker.analyse.algorithm.NameAndSourceAttributes;
import org.apache.osgimaker.analyse.algorithm.graph.AtomicVertex;
import org.apache.osgimaker.analyse.algorithm.graph.StrongComponent;


/**
 * Renderer of an {@link AtomicVertex} with 
 * {@link ClassAttributes}. The renderer is based on a 
 * <tt>java.text.MessageFormat</tt> template. The variables in the
 * template have the following meaning:
 * <table border=1 cellpadding=5>
 * <tr><th>Variable index</th><th>Description</th></tr>
 * <tr><td>0</td><td>fully-qualified class name</td></tr>
 * <tr><td>1</td><td>class type</td></tr>
 * <tr><td>2</td><td>size of the class file in bytes</td></tr>
 * <tr><td>3</td>
 *     <td><tt>true</tt> if inner class otherwise <tt>false</tt></td></tr>
 * <tr><td>4</td><td>Number of incoming arcs</td></tr>
 * <tr><td>5</td>
 *     <td>Number of outgoing arcs to other vertices in the graph</td></tr>
 * <tr><td>6</td><td>Number of outgoing arcs to external vertices</td></tr>
 * <tr><td>7</td><td>Layer index</td></tr>
 * <tr><td>8</td><td>Name of the cycle or empty string</td></tr>
 * <tr><td>9</td><td>Source of class file if known</td></tr>
 * </table>
 *
 */
public class TemplateBasedClassRenderer implements AtomicVertexRenderer 
{
  private MessageFormat _format;

  /** Creates an instance for the specified template. */
  public TemplateBasedClassRenderer(String template) 
  {
    _format = new MessageFormat(template);
  }

  /**
   * Renderes the specified vertex. It is assumed that the vertex attributes
   * are of the type {@link org.apache.osgimaker.analyse.algorithm.ClassAttributes}.
   * @param vertex Vertex to be rendered.
   * @return the rendered vertex.
   */
  public String render(AtomicVertex vertex, StrongComponent cycle, 
                       int layerIndex) 
  {
    String[] values = new String[10];
    NameAndSourceAttributes attributes = (NameAndSourceAttributes) vertex.getAttributes();
    values[0] = attributes.getName();
    values[2] = Integer.toString(attributes.getSize());
    values[9] = attributes.getSources();
    if (attributes instanceof ClassAttributes)
    {
      ClassAttributes ca = (ClassAttributes) attributes;
      values[1] = ca.getType();
      values[3] = ca.isInnerClass() ? "true" : "false";
    } else
    {
      values[1] = "";
      values[3] = "";
    }
    values[4] = Integer.toString(vertex.getNumberOfIncomingArcs());
    int usesInternal = 0;
    int usesExternal = 0;
    for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++) 
    {
      if (((AtomicVertex) vertex.getHeadVertex(i)).isGraphVertex()) 
      {
        usesInternal++;
      } else 
      {
        usesExternal++;
      }
    }
    values[5] = Integer.toString(usesInternal);
    values[6] = Integer.toString(usesExternal);
    values[7] = Integer.toString(layerIndex);
    values[8] = cycle == null ? "" 
                          : AbstractStrongComponentRenderer.createName(cycle); 
    return _format.format(values);
  }
}